<template>
  <div class="radish-sprite">
    <div class="radish-wrapper">
      <div class="radish-house">
        <div class="windmill"><img src="https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/windmill.png" alt=""></div>
      </div>
      <div class="radish-cloud">
        <div class="cloud cloud3"><img src="https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/cloud3.png" alt=""></div>
        <div class="cloud cloud1"><img src="https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/cloud1.png" alt=""></div>
        <div class="cloud cloud3"><img src="https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/cloud3.png" alt=""></div>
        <div class="cloud cloud2"><img src="https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/cloud2.png" alt=""></div>
        <div class="cloud cloud3"><img src="https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/cloud3.png" alt=""></div>
      </div>
      <div class="radish-wrapper-top">
        <div class="wrapper-left">
          <div class="wx-info">
            <img @click="handleClickToHome" :src="userInfo.head_img || defaultAvatar" alt="">
            <span class="name">{{ userInfo.nickname || '游客3312' }}</span>
          </div>
          <div class="activity">
            <span>{{ actTitle || '活动未开启' }}</span>
          </div>
        </div>
        <div class="wrapper-right fr">
          <div class="wrapper-right-item">
            <div class="icon"><img class="icon1" src="https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/icon_time.png" alt=""></div>
            <span class="txt"> {{ userActInfo.luobo_times || 0 }} </span>
          </div>
          <div class="wrapper-right-item">
            <div class="icon"><img class="icon2" src="https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/icon_radish.png" alt=""></div>
            <span class="txt">{{ userActInfo.luobo_num || 0}}</span>
          </div>
          <div class="wrapper-right-item" @click="handleClickGiftDialog">
            <div class="icon"><img class="icon3" src="https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/icon_gift.png" alt=""></div>
            <span class="txt">兑换</span>
          </div>
          <div class="wrapper-right-item" @click="showIntroduction = true">
            <div class="icon"><img class="icon3" src="https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/icon_introduction.png" alt=""></div>
            <span class="txt">攻略</span>
          </div>
        </div>
      </div>
      <div class="radish-wrapper-middle">
        <div class="radish-land">
          <div v-for="(item, index) in classList" :key="index" class="radish-land-item" :class="item"></div>
        </div>
      </div>
      <div class="radish-wrapper-bottom">
        <div class="radish-sprite-item" @click="handleClickShowDialog">
          <img class="animated infinite" src="https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/radishSprite.png" alt="">
          <div class="click-tips">
            <img src="https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/click.png" alt="">
          </div>
        </div>
      </div>
    </div>
    <prize-dialog
      ref="prizeDialogs"
      @refreshFn="refreshFn"
      :addLuoboActNums="add_luobo_act_nums"
      :radishNum="Number(userActInfo.luobo_num)"
      :exchangePointRate="Number(exchangePointRate)"
      :prizeRadishNum="prizeRadishNum"
      :itemList="exchangeItem"></prize-dialog>

    <van-popup v-model="showIntroduction" :style="{ background: 'transparent' }">
      <div class="introduction">
        <div class="introduction-wrap">
          <div class="introduction-wrap-item">
            1、萝卜：萝卜可以用来兑换积分或者商品
          </div>
          <div class="introduction-wrap-item">
            2、拔萝卜：会员每天可获得一定数量拔萝 <br>卜次数，每拔一次可获得0~9个萝卜
          </div>
          <div class="introduction-wrap-item" v-for="(item,index) in add_luobo_act_nums" :key="index">
            {{index+3}}、 {{item}}
          </div>
          <div class="introduction-wrap-btn">
            <div class="btn" @click="showIntroduction = false">
              <img class="animated infinite" src="https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/btn_introduction.png" alt="">
            </div>
          </div>
        </div>
      </div>
    </van-popup>
    <share-shade ref="shareShades"></share-shade>
    <!-- 授权弹出框 -->
    <small-login ref="smalllogin"></small-login>
  </div>
</template>

<script>
  /**
	 * @Author: yqe@360shop.cn
	 * @Date: 2019-10-21 18:48:09
	 * @Last Modified by: yqe@360shop.cn
	 * @Last Modified time: 2019-10-21 18:48:09
	 */
  import Vue from 'vue'
  import { radishDetail, radishPulling } from '@/api/marketGame/radishSprite'
  import { mapGetters } from 'vuex'
  import prizeDialog from '../prizeDialog'
  import { mpShare } from '@/utils/utils'
  import small from '@/smallapp/small'
  import SmallLogin from '@/components/SmallLogin/smallLogin.vue'
  import shareShade from '@/components/wxShare/shareShade'

  export default Vue.extend({
    name: 'index',
    components: {
      prizeDialog,
      SmallLogin,
      shareShade
    },
    computed: {
      ...mapGetters(['wxConfig'])
    },
    data() {
      return {
        showIntroduction: false,
        defaultAvatar: 'https://img.wifenxiao.com/h5-wfx/images/default_avatar.jpg',
        add_luobo_act_nums: [],
        userActInfo: {
          luobo_num: 0
        },
        exchangeItem: [],
        exchangePointRate: undefined,
        prizeRadishNum: 0,
        actTitle: '', // 活动标题
        classList: [
          'radish1', 'radish2', 'radish3', 'radish4', 'radish5', 'radish6', 'radish7', 'radish8', 'radish9'
        ],
        timer: null,
        userInfo: {},
      }
    },
    methods: {
      // 点击头像跳转到商城首页
      handleClickToHome() {
        // TODO 小程序跳转处理
          window.location.href = '/index'

      },
      /**
       * 开出宝箱后刷新数据
       */
      refreshFn() {
        this.radishDetailPost()
      },
      /**
       * 获取萝卜信息
       */
      radishDetailPost() {
        radishDetail().then(res => {
          if (res.status === 1) {
            this.userInfo = {
              head_img: res.data.avthor,
              nickname: res.data.nick
            }
            const arr = []
            this.exchangePointRate = res.data.exchange_point_rate
            this.userActInfo = res.data.user_act_info
            this.actTitle = res.data.title
            this.add_luobo_act_nums = res.data.add_luobo_act_nums
            let item
            if (res.data.exchange_item.item) {
              item = res.data.exchange_item.item
            } else {
              item = {}
            }
            item.exchangeNum = res.data.exchange_item.exchange_num
            arr.push(item)
            this.exchangeItem = arr
            this.shareInfo = {
              title: res.data.jsapi_title,
              imgUrl: res.data.jsapi_img,
              shareParam: res.data.jsapi_url.split('?')[1]
            }
            mpShare(true, false, '/activity/pages/radishSprite/index', this.shareInfo)
          } else {
            if (res.msg === '活动未开启') {
              this.$refs.prizeDialogs.showNoStartFn()
            }
          }
        })
      },
      /**
       * 点击萝卜
       */
      handleClickShowDialog(param) {
        window.clearTimeout(this.timer)
        const times = this.userActInfo.luobo_times || 0
        this.$refs.prizeDialogs.showDialogVisible = true
        if (parseInt(times) <= 0) {
          this.$refs.prizeDialogs.showNoTime = true
          this.$refs.prizeDialogs.disableClick = false
        } else {
          radishPulling().then(res => {
            // // console.log(res)
            if (res.status === 1) {
              if (res.data.luobo_num !== 0) {
                this.$refs.prizeDialogs.showPrizeFn()
                this.prizeRadishNum = res.data.luobo_num ? parseInt(res.data.luobo_num) : 0
              } else {
                this.$refs.prizeDialogs.showNoPrizeFn()
              }
            } else {
              this.prizeRadishNum = 0
            }
            this.timer = setTimeout(() => {
              this.$refs.prizeDialogs.handleClickDialogClose()
            }, 10000)
          })
        }
      },
      /**
       * 展开兑换的弹层
       */
      handleClickGiftDialog() {
        this.$refs.prizeDialogs.showDialogVisible = true
        this.$refs.prizeDialogs.showGiftDialog = true
        this.$refs.prizeDialogs.disableClick = false // 遮罩可立刻点击
        this.$refs.prizeDialogs.dialogType = 'giftDialog'
      }
    },
    mounted() {
      this.radishDetailPost()
      window.addEventListener('wxshow', () => {
        small.HandleShareParams()
        if(!small.checkLogin()) {
          this.$refs.smalllogin.openSmallLoginPop()
        }
      })
      window.$$subscribe('loginReload', reload => {
        if (reload) {
          this.radishDetailPost()
        }
      })
    }
  })
</script>
<style lang="scss">
  @import "src/styles/mixin";
  @import "src/styles/transition";
  .radish-sprite{
    .radish-wrapper{
      position: relative;
      bottom: 0;
      width: 100%;
      height: 100vh;
      overflow: hidden;
      background: url('https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/bg.png') center no-repeat;
      background-size: 100% 100%;
      .radish-house{
        position: absolute;
        right: 8px;
        top: 50%;
        transform: translateY(-77%);
        width: 249px;
        height: 278px;
        background: url('https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/house.png') center no-repeat;
        background-size: 100% 100%;
        z-index: 1;
        .windmill{
          width: 100%;
          position: relative;
          img{
            animation:rotate 4s linear infinite;
            /*transform-origin:middle;*/
            position: absolute;
            width: 130px;
            top: -50px;
            left: 5px;
          }
        }
      }
      .radish-cloud{
        position: absolute;
        top: 30%;
        left: 132px;
        white-space: nowrap;
        .cloud{
          animation:cloudMove 120s linear infinite;
          display: inline-block;
          position: relative;
        }
        .cloud1{
          margin-left: 20px;
          margin-right: 80px;
          img{
            width: 135px;
            height: 52px;
          }
        }
        .cloud2{
          top: -10px;
          img{
            width: 88px;
            height: 39px;
          }
        }
        .cloud3{
          top: 20px;
          img{
            width: 56px;
            height: 21px;
          }
        }
      }
      &-top{
        .wrapper-left{
          width: 300px;
          padding: 20px 0 20px 17px;
          border-radius: 12px;
          background: rgba(0,119,213,.58);
          display: inline-block;
          margin-left: 25px;
          margin-top: 40px;
          border: 2px solid rgba(255, 255, 255, 0.09);
          box-shadow: 4px 4px 20px rgba(0, 0, 0, .15);
          color: #fff;
          .wx-info{
            font-size: 0;
            margin-bottom: 20px;
            img{
              @include alignTop;
              width: 60px;
              height: 60px;
              border: 4px solid #eb9526;
              border-radius: 50%;
            }
            .name{
              @include alignTop;
              @include showEllipsis;
              width: 200px;
              line-height: 60px;
              font-size: 32px;
              color: #fff;
              margin-left: 10px;
            }
          }
          .activity{
            span{
              width: 100%;
              font-size: 26px;
              @include showEllipsis;
              @include alignTop;
              margin-top: -2px;
            }
          }
        }
        .wrapper-right{
          display: inline-block;
          margin: 30px 20px 0 0;
          &-item{
            width: 158px;
            height: 66px;
            line-height: 58px;
            background: rgba(0, 119, 213, 1);
            border-radius: 80px;
            margin-bottom: 22px;
            font-size: 0;
            border: 4px solid #30b3fa;
            .icon{
              width: 60px;
              text-align: right;
              height: 58px;
              margin-right: 4px;
              @include alignTop;
              .icon1{
                width: 43px;
                margin-top: 10px;
              }
              .icon2{
                width: 37px;
                margin-top: -10px;
              }
              .icon3{
                width: 41px;
                margin-top: 8px;
              }
            }
            .txt{
              @include alignTop;
              color: #fff;
              font-size: 30px;
              width: 70px;
              text-align: center;
            }
          }
        }
      }
      &-middle{
        position: absolute;
        width: 100%;
        bottom: 180px;
        z-index: 1;
        .radish-land{
          position: relative;
          width: 664px;
          height: 540px;
          background: url('https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/land.png') center no-repeat;
          background-size: 100% 100%;
          margin: 0 auto;
          &-item{
            position: absolute;
            width: 102px;
            height: 142px;
            background: url('https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/radish.png') center no-repeat;
            background-size: 100% 100%;
            animation:move 1s linear infinite;
            transform-origin:bottom;
            &.radish1{
              left: 280px;
              top: 100px;
            }
            &.radish2{
              left: 165px;
              top: 170px;
            }
            &.radish3{
              left: 55px;
              top: 230px;
            }
            &.radish4{
              left: 395px;
              top: 170px;
            }
            &.radish5{
              left: 280px;
              top: 240px;
            }
            &.radish6{
              left: 165px;
              top: 295px;
            }
            &.radish7{
              left: 500px;
              top: 230px;
            }
            &.radish8{
              left: 390px;
              top: 290px;
            }
            &.radish9{
              left: 285px;
              top: 355px;
            }
          }
        }
      }
      &-bottom{
        position: absolute;
        bottom: 0;
        z-index: 3;
        .radish-sprite-item{
          img{
            width: 245px;
            margin: 0 0 40px 40px;
          }
          .click-tips{
            position: absolute;
            right: 0;
            bottom: 0;
            animation:clickMove 3s linear infinite;
            img{
              width: 74px;
            }
          }
        }
      }
    }
    .introduction{
      width: 620px;
      height: 720px;
      background: url('https://img.wifenxiao.com/h5-wfx/images/marketGame/radishSprite/new_bg_introduction.png') center no-repeat;
      background-size: 100% 100%;
      &-wrap{
        padding-top: 250px;
        &-item{
          font-size: 26px;
          color: #FEF5D2;
          line-height: 40px;
          padding-left: 60px;
          margin-bottom: 20px;
        }
        &-btn{
          padding-top: 10px;
          text-align: center;
          img{
            width: 296px;
          }
        }
      }
    }
  }

</style>
